﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI
{
    /// <summary>
    /// Interaction logic for EditWindow.xaml
    /// </summary>
    public partial class EditWindow : Window
    {
        private string defaultInfoCacheLocation = "{0}\\Users\\{1}\\AppData\\Roaming\\Microsoft\\eHome\\DvdInfoCache";
        private BLL.InfoCache currentinfocache = null;

        public string InfoCacheLocation { get; set; }
        public string DvdId { get; set; }

        public EditWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.DvdId.Length == 0)
            {
                throw new ApplicationException("DvdId has not been set");
            }

            this.Title = string.Format(this.Title, this.DvdId);

            string currentUser = System.Environment.UserName;
            string osDrive = System.Environment.GetEnvironmentVariable("SystemDrive");
            string dvdInfoCacheDirectory = string.Format(this.defaultInfoCacheLocation, osDrive, currentUser);

            string dvdInfoCacheXmlLocation = string.Format("{0}\\{1}.xml", dvdInfoCacheDirectory, this.DvdId.Replace("|", "-"));

            if (!System.IO.File.Exists(dvdInfoCacheXmlLocation))
            {
                MessageBox.Show("The DvdInfoCache file could not be found");
                return;
            }

            BLL.InfoCache infoCache = new BLL.InfoCache();
            infoCache.DvdInfoCacheFilePath = dvdInfoCacheXmlLocation;
            UTIL.Xml.ReadDvdInfoCacheXml(ref infoCache);

            this.txtMovieTitle.Text = infoCache.Title;

            if (infoCache.IsLargeCoverParamsSet == false)
            {
                this.chkLargeCoverParam.IsEnabled = false;
            }
            else
            {
                this.chkLargeCoverParam.IsChecked = true;
            }

            if (infoCache.IsSmallCoverParamsSet == false)
            {
                this.chkSmallCoverParam.IsEnabled = false;
            }
            else
            {
                this.chkSmallCoverParam.IsChecked = true;
            }

            this.currentinfocache = infoCache;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if ((this.chkLargeCoverParam.IsEnabled && this.chkLargeCoverParam.IsChecked == true) || (this.chkSmallCoverParam.IsEnabled && this.chkSmallCoverParam.IsChecked == true))
            {
                MessageBoxResult removalResult = MessageBox.Show("Removal of the CoverParam tags from the Xml cannot be undone.\r\n\r\nContinue?", "DvdIdRetriever", MessageBoxButton.YesNo);
                if (removalResult == MessageBoxResult.No)
                {
                    return;
                }
            }

            if (currentinfocache != null)
            {
                UTIL.Xml.EditDvdInfoCacheXml(currentinfocache, this.txtMovieTitle.Text, this.chkLargeCoverParam.IsChecked.Value, this.chkSmallCoverParam.IsChecked.Value);
            }

            this.Close();
        }
    }
}
